package com.example.administrator.trains;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import java.io.IOException;

public class administrate_myorder extends Activity {

    String vehicleNum;
    String startPlace;
    String endPlace;
    String startTime;
    String endTime;
    String price;
    String date;
    String id;
    String ticket_type;

    Handler myHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if(msg.what==1){
                Toast.makeText(administrate_myorder.this,"退票成功,可以查看订单哦~~",Toast.LENGTH_LONG).show();
            }
            if(msg.what==2){
                Intent i = new Intent(administrate_myorder.this, show_myorder.class);
                i.putExtra("date", date);
                i.putExtra("startPlace", startPlace);
                i.putExtra("endPlace", endPlace);
                i.putExtra("id", id);
                i.putExtra("vehicleNum", vehicleNum);
                startActivity(i);
            }
            if(msg.what==3){
                Toast.makeText(administrate_myorder.this,"没有找到对应的车票,请联系管理员",Toast.LENGTH_LONG).show();
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(getWindow().FEATURE_NO_TITLE);
        setContentView(R.layout.activity_administrate_myorder);

        Intent i = getIntent();
        vehicleNum = i.getStringExtra("orderNum");
        startPlace = i.getStringExtra("startPlace");
        endPlace = i.getStringExtra("endPlace");
        startTime = i.getStringExtra("startTime");
        endTime = i.getStringExtra("endTime");
        date = i.getStringExtra("date");
        price = i.getStringExtra("price");
        ticket_type = i.getStringExtra("type");
        id = i.getStringExtra("id");

        final TextView czh_tv1 = (TextView)findViewById(R.id.from_station);  //开始地
        final TextView czh_tv4 = (TextView)findViewById(R.id.to_station);  //目的地
        final TextView czh_tv2 = (TextView)findViewById(R.id.start_time);   //开始时间
        final TextView czh_tv16 = (TextView)findViewById(R.id.end_time); //到达时间
        final TextView czh_tv14 = (TextView)findViewById(R.id.train_num);  //车次
        final TextView czh_tv13 = (TextView)findViewById(R.id.date);  //日期
        final TextView czh_tv12 = (TextView)findViewById(R.id.ticket_price); //票价
        final TextView czh_tv15 = (TextView)findViewById(R.id.ticket_type); //票价

        czh_tv1.setText(startPlace.trim());
        czh_tv4.setText(endPlace.trim());
        czh_tv2.setText(startTime.trim());
        czh_tv16.setText(endTime.trim());
        czh_tv14.setText(vehicleNum.trim());
        czh_tv12.setText((price+"元").trim());
        czh_tv13.setText(date.trim());
        czh_tv15.setText(ticket_type.trim());

        Button change_ticket = (Button)findViewById(R.id.bt_change_ticket); //改签
        Button return_ticket = (Button)findViewById(R.id.bt_return_ticket); //退票

        //退票操作
        return_ticket.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    v.setBackgroundDrawable(getResources().getDrawable(R.drawable.pswshape2));
                } else if (event.getAction() == MotionEvent.ACTION_UP) {
                    v.setBackgroundDrawable(getResources().getDrawable(R.drawable.pswshape));

                    try {
                        Socket.connector.send(Socket.connector.package_data(Socket.connector.TICKET_CMD_TYPE,
                                Socket.connector.RETURN_TICKET, "00",
                                        id.trim() + CommunicationData.empty +
                                        vehicleNum.trim() + CommunicationData.empty
                                        ));
                        Socket.connector.recv_ack();
                        if (CommunicationData.ack == "01") {
                            Socket.connector.recv_ack();
                            if (CommunicationData.ack == Socket.connector.RETURN_TICKET_SUCCESS_ACK) {
                                Message m = new Message();
                                m.what = 1;
                                myHandler.sendMessage(m);
                            } else if (CommunicationData.ack == Socket.connector.NO_FIND_TICKET_ACK) {
                                Message m = new Message();
                                m.what = 3;
                                myHandler.sendMessage(m);
                            }
                        }
                    } catch (IOException e) {
                    }
                }
                return true;
            }
        });

        //改签操作
        change_ticket.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    v.setBackgroundDrawable(getResources().getDrawable(R.drawable.pswshape2));
                } else if (event.getAction() == MotionEvent.ACTION_UP) {
                    v.setBackgroundDrawable(getResources().getDrawable(R.drawable.pswshape));

                    try {
                        /*改签第一步：先发送查找车票请求*/
                        Socket.connector.send(Socket.connector.package_data(Socket.connector.TICKET_CMD_TYPE,
                                Socket.connector.USER_FIND_TICKET, "00",
                                        startPlace.trim() + CommunicationData.empty +
                                        endPlace.trim() + CommunicationData.empty +
                                        date.trim() + CommunicationData.empty));
                        Socket.connector.recv_ack();
                        if (CommunicationData.ack == "01") {
                            Message m = new Message();
                            m.what = 2;
                            myHandler.sendMessage(m);
                        } else {
                            Message m = new Message();
                            m.what = 3;
                            myHandler.sendMessage(m);
                        }
                    } catch (IOException e) {
                    }

                }
                return true;
            }
        });
    }
}
